import axios from "./axios";
import $filter from '@/common/vueFilter';
import $api from '@/common/api';
import md5 from 'js-md5'
import {
    Toast
} from 'vant';

export default {
    post: async (url, _params) => {
        try {
            let res = await axios.post(url, _params);
            if (res.code == 200) {
                return res;
            } else {
                Toast.fail(res.message);
                return;
            }
        } catch (error) {
            console.error(error);
            return;
        }
    },
    get: async (url, _params) => {
        let params = {
            params: _params
        };
        try {
            let res = await axios.get(url, params);
            if (res.code == 200) {
                return res;
            } else {
                Toast.fail(res.msg);
                return;
            }
        } catch (error) {
            console.error(error);
            return;
        }
    },
    checkPost: async (url, _params) => {
        let params = {
            ..._params,
            timestamp: parseInt(new Date().getTime() / 1000)
        };
        let newParams = $filter.JsonSort(params);
        let paramsString = "";
        Object.keys(newParams).forEach(key => {
            paramsString += newParams[key];
        });
        let paramsMd5 = md5(paramsString + $api.key).toUpperCase();
        params = {
            ..._params,
            timestamp: parseInt(new Date().getTime() / 1000),
            sign: paramsMd5
        };
        try {
            let res = await axios.post(url, params);
            if (res.code == 200) {
                return res;
            } else {
                Toast.fail(res.message);
                return;
            }
        } catch (error) {
            console.error(error);
            return;
        }
    },

}